package com.lqian.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 有一个 Student 的 list，你想把它转换成一个 map，key 是 student 对象的 id，value 就是 student 对象本身。
 */
public class StudentTest {

    public static void main(String[] args) {
        List<Student> stuList = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            stuList.add(new Student(i+1,"jack",18+i%8));
        }
//        System.out.print(stuList);

        /**
         * Student::getId 对象::成员方法
         */
        /**
         *    public static <T, K, U>
         *     Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
         *                                     Function<? super T, ? extends U> valueMapper,
         *                                     BinaryOperator<U> mergeFunction) {
         *         return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
         *     }
         */
        Map<Integer, Student> map = stuList.stream().collect(Collectors.toMap(Student::getId, a -> a, (a,b) -> a));
        Set set= map.keySet();
        for (Object k : set) {
            System.out.println(k + "-" + map.get(k));
        }
        /**
         * @FunctionalInterface
         * public interface Function<T, R> {
         *    R apply(T t);
         *    ...
         *    static <T> Function<T, T> identity() {
         *        return t -> t;
         *    }
         * }
         */
    }
}
